# -*-coding: UTF-8 -*-

import os, cv2, time, shutil, copy, sys
from videoLabel import videoLabel as vl
import numpy as np

import random

def mkpath(path):
    if (os.path.exists(path)):
        return path
    os.mkdir(path)
    return path


def renameLabel(labelPath):
    labelLoc = []
    for file in os.listdir(labelPath):
        if file.endswith("txt") and not file.startswith("cla"):
            with open(os.path.join(labelPath, file), 'r') as f:
                lines = f.readlines()
                for line in lines:
                    line = line.rstrip()
                    label, x, y, w, h = list(map(float, line.split(" ")))
                    if label == 1:
                        label = 0
                        ll = " ".join(map(str, [label, x, y, w, h]))
                        labelLoc.append(ll)
                    else:
                        ll = line
                        labelLoc.append(line)
                    print(ll)
            # with open(os.path.join(labelPath,file),'w') as f:
            #     for ll in labelLoc:
            #         f.writelines(ll+"\n")
        labelLoc = []

def checklabel(src, dst):
    labels = []
    with open("/mnt/data/people/classes.txt",'r') as f:
        lines = f.readlines()
        for line in lines:
            labels.append(line.rstrip())

    mkpath(dst)
    map(mkpath, [os.path.join(dst, i.rstrip()) for i in labels])

    for i in os.listdir(src):
        print(i)
        if (i.startswith("cla") or i.startswith("train")):
            continue

        if (i.endswith("g") or i.endswith("G")):
            imgPath = os.path.join(src, i)
            txtName = i.split(".")[0] + ".txt"
            txtPath = os.path.join(src, txtName)
            # if (os.path.isfile(txtPath)):
            #     with open(txtPath, 'r') as f:
            #         lines = f.readlines()
            #         if (len(lines) == 0):
            #             try:
            #                 # os.remove(imgPath)
            #                 # os.remove(txtPath)
            #                 print(imgPath, " __________ has removed.")
            #             except:
            #                 continue
            #             continue
            #
            # if ((not os.path.exists(txtPath)) or (not os.path.exists(imgPath))):
            #     try:
            #         # os.remove(imgPath)
            #         # os.remove(txtPath)
            #         print(imgPath, " __________ has removed.")
            #     except:
            #         continue
            #
            # if (not os.path.isfile(txtPath)):
            #     continue

            with open(txtPath, 'r') as f:
                img = cv2.imread(imgPath)
                imgTmp = img.copy()
                _, iw, ih = img.shape[::-1]
                lines = f.readlines()
                for num, line in enumerate(lines):
                    label, xr, yr, wr, hr = list(map(float, line.rstrip().split(" ")))

                    x, y, w, h = vl.convertR(xr, yr, wr, hr, iw, ih)
                    x, y, w, h = list(map(int, [x, y, w, h]))
                    # if(int(label) == 0):
                    cv2.rectangle(imgTmp, (x, y), (x + w, y + h), (0, 0, 255), 4, 4)
                    roi = img[slice(y, y + h), slice(x, x + w)]
                    labelPath = os.path.join(os.path.join(dst, labels[int(label)]),
                                             i.split(".")[0] + "_" + str(num).zfill(6) + ".jpg")
                    cv2.imwrite(labelPath, roi)


                    # if (int(label) == 1):
                    #     cv2.rectangle(imgTmp, (x, y), (x + w, y + h), (0, 255, 0), 4, 4)
                    #     roi = img[slice(y, y + h), slice(x, x + w)]
                    #     labelPath = os.path.join(os.path.join(dst, labels[int(label)]),i.split(".")[0] + "_" + str(num) + ".jpg")
                    #     cv2.imwrite(labelPath, roi)

            imgTmp = cv2.resize(imgTmp, (960, 540))
            cv2.imshow("img", imgTmp)
            cv2.waitKey(1)

def rename(src, dst):
    for i in os.listdir(src):
        if ("-" in i):
            newName = i.replace("-", "")
            shutil.copy(os.path.join(src, i), os.path.join(dst, newName))
        else:
            shutil.copy(os.path.join(src, i), os.path.join(dst, i))

def renameEnds(src):
    imgList = []
    for i, file in enumerate(os.listdir(src)):
        img = cv2.imread(os.path.join(src, file))
        cv2.imwrite(os.path.join("/mnt/data/Pyproject/searchGarbage/downLoadsImg/1025kerasAgu",
                                 "1025" + str(i).zfill(6)) + ".jpg", img)

        # imgList.append(img)
        print(file, i)
        # os.remove(os.path.join(src,file))

    # for num,img in enumerate(imgList):
    #     cv2.imwrite(os.path.join(src,"1025"+str(num).zfill(6))+".jpg",img)

def makeYoloTrainDataFormat(src, dst=""):
    timeToday = str(time.localtime().tm_mon) + "_" + str(time.localtime().tm_mday)
    # timeToday="9_23"
    trainName ="Train" + src.split("/")[-1] +  timeToday + ".txt"
    valName = "Val" +src.split("/")[-1] +  timeToday + ".txt"
    print(trainName, valName)
    print(os.path.join(os.path.abspath(src), os.listdir(src)[0]))
    # return
    trainTxt = open(os.path.join(src, trainName), "a")
    valTxt = open(os.path.join(src, valName), "a")
    ii = 0

    total = 0
    for i in os.listdir(src):
        if not i.endswith("txt"):
            total += 1

    srcList = os.listdir(src)
    random.shuffle(srcList)
    for num, i in enumerate(srcList):
        if (i.endswith("txt")):
            continue
        imgName = i
        imgTxt = imgName.split(".")[0] + ".txt"
        if (not os.path.isfile(os.path.join(os.path.abspath(src), i))):
            print(imgTxt)
            ii += 1

        if (num / 2 < total * 0.9):
            # pass
            trainTxt.write(os.path.join(os.path.abspath(src), i) + "\n")
        else:
            # pass
            valTxt.write(os.path.join(os.path.abspath(src), i) + "\n")
        print(num, i)


    trainTxt.close()
    valTxt.close()
    if (dst != ""):
        # pass
        os.system("cat " + os.path.join(src,trainName) + ">>" + os.path.join(dst , timeToday + "_train.txt") )
        os.system("cat " + os.path.join(src,valName) + ">>" + os.path.join(dst , timeToday + "_val.txt"))


def tmp():
    cap = cv2.VideoCapture(1)
    ret, img = cap.read()
    while (ret):
        ret, img = cap.read()
        cv2.imshow("img", img)
        cv2.waitKey(1)

def videoLabel():
    maxBox = [150, 220]
    minBox = [60, 80]
    video = "/mnt/data/videos/0903"
    # for i in os.listdir(video):
    # videoName_ = int("".join(list(i.split(".")[0])[:6]))
    # if(videoName_<=459):
    #     continue

    # videoPath = os.path.join(video,i)
    videoPath = "/mnt/data/videos/000400.mp4"
    # maxBox = [250, 350]
    # minBox = [60, 80]
    # loc = [720, 100, 1180, 800]

    # videoPath = "/media/tsl/windows/RC/视频1015/国泰路22号附61号枪机_1571137029/国泰路22号附61号枪机_066B9DF0_1571137031_1.mp4"
    # maxBox = [250, 300]
    # minBox = [60, 60]
    # loc = [1050, 60, 860, 700]

    # videoPath = "/media/tsl/windows/RC/视频1015/国泰路22号附69号球机_1571138131/国泰路22号附69号球机_1C572238_1571138133_1.mp4"
    # maxBox = [180, 230]
    # minBox = [50, 50]
    # loc = [310, 600, 860, 250]

    # videoPath = "/media/tsl/windows/RC/0903/000055.mp4"
    # maxBox = [200, 330]
    # minBox = [50, 70]
    # loc = [20, 200, 1245, 700]

    # videoPath = "/media/tsl/windows/RC/视频1015/廉租房西正大门球机_1571136207/廉租房西正大门球机_066B9DF0_1571136454_1.mp4"
    # maxBox = [200, 330]
    # minBox = [80, 100]
    # loc = [400, 680, 1300, 700]

    # videoPath = "/media/tsl/windows/RC/0903/000065.mp4"
    # maxBox = [120, 250]
    # minBox = [60, 90]
    # loc = [20, 120, 1200, 700]

    # videoPath = "/media/tsl/windows/RC/0903/000113.mp4"
    # maxBox = [120, 200]
    # minBox = [60, 90]
    # loc = [800, 500, 800, 500]

    # videoPath = "/media/tsl/windows/RC/0903/000071.mp4"
    # maxBox = [150, 200]
    # minBox = [60, 80]
    # loc = [30, 200, 900, 660]

    # videoPath = "/media/tsl/windows/RC/0903/000122.mp4"
    # maxBox = [120, 200]
    # minBox = [60, 80]
    # loc = [10, 60, 800, 570]

    # videoPath = "/media/tsl/windows/RC/0903/000158.mp4"
    # maxBox = [100, 120]
    # minBox = [50, 70]
    # loc = [150, 120, 1180, 330]

    # videoPath = "/media/tsl/windows/RC/0903/000164.mp4"
    # maxBox = [120, 160]
    # minBox = [50, 70]
    # loc = [220, 160, 1280, 400]

    # videoPath = "/media/tsl/windows/RC/0903/000165.mp4"
    # maxBox = [120, 160]
    # minBox = [50, 70]
    # loc = [220, 160, 1280, 400]

    # videoPath = "/media/tsl/windows/RC/0903/000180.mp4"
    # maxBox = [100, 150]
    # minBox = [40, 70]
    # loc = [10, 100, 1260, 600]

    # videoPath = "/media/tsl/windows/RC/0903/000181.mp4"
    # maxBox = [100, 150]
    # minBox = [40, 70]
    # loc = [10, 100, 1260, 600]

    # videoPath = "/media/tsl/windows/RC/0903/000182.mp4"
    # maxBox = [100, 150]
    # minBox = [40, 70]
    # loc = [10, 100, 1260, 600]

    # videoPath = "/media/tsl/windows/RC/0903/000186.mp4"
    # maxBox = [100, 150]
    # minBox = [60, 90]
    # loc = [10, 100, 1260, 600]

    # videoPath = "/media/tsl/windows/RC/0903/000188.mp4"
    # maxBox = [100, 150]
    # minBox = [60, 90]
    # loc = [10, 100, 1260, 600]

    # videoPath = "/media/tsl/windows/RC/0903/000189.mp4"
    # maxBox = [100, 150]
    # minBox = [60, 90]
    # loc = [10, 100, 1260, 600]

    # videoPath = "/media/tsl/windows/RC/0903/000211.mp4"
    # maxBox = [100, 170]
    # minBox = [70, 70]
    # loc = [1100, 400, 780, 530]

    # videoPath = "/media/tsl/windows/RC/0903/000212.mp4"
    # maxBox = [100, 150]
    # minBox = [70, 90]
    # loc = [30, 450, 450, 600]

    # videoPath = "/media/tsl/windows/RC/0903/000238.mp4"
    # maxBox = [130, 170]
    # minBox = [70, 90]
    # loc = [720, 200, 1160, 500]

    # videoPath = "/media/tsl/windows/RC/0903/000246.mp4"
    # maxBox = [130, 170]
    # minBox = [70, 90]
    # loc = [720, 200, 1160, 500]

    # videoPath = "/media/tsl/windows/RC/0903/000251.mp4"
    # maxBox = [130, 170]
    # minBox = [70, 90]
    # loc = [50, 470, 700, 600]

    # videoPath = "/media/tsl/windows/RC/0903/000256.mp4"
    # maxBox = [120, 150]
    # minBox = [40, 60]
    # loc = [830, 170, 810, 490]

    # videoPath = "/media/tsl/windows/RC/0903/000279.mp4"
    # maxBox = [120, 140]
    # minBox = [50, 60]
    # loc = [460, 100, 870, 420]

    # videoPath = "/media/tsl/windows/RC/0903/000283.mp4"
    # maxBox = [120, 140]
    # minBox = [50, 60]
    # loc = [460, 100, 870, 420]

    # videoPath = "/media/tsl/windows/RC/0903/000322.mp4"
    # maxBox = [200, 250]
    # minBox = [80, 60]
    # loc = [1000, 100, 700, 950]

    # videoPath = "/media/tsl/windows/RC/0903/000353.mp4"
    # maxBox = [100, 130]
    # minBox = [50, 70]
    # loc = [10, 300, 840, 380]

    # videoPath = "/media/tsl/windows/RC/0903/000355.mp4"
    # maxBox = [100, 130]
    # minBox = [50, 70]
    # loc = [840, 300, 840, 380]

    # videoPath = "/media/tsl/windows/RC/0903/000360.mp4"
    # maxBox = [100, 130]
    # minBox = [50, 70]
    # loc = [10, 300, 840, 380]

    # videoPath = "/media/tsl/windows/RC/0903/000361.mp4"
    # maxBox = [100, 130]
    # minBox = [50, 70]
    # loc = [840, 300, 840, 380]

    # videoPath = "/media/tsl/windows/RC/0903/000362.mp4"
    # maxBox = [100, 130]
    # minBox = [50, 70]
    # loc = [10, 300, 840, 380]

    # videoPath = "/media/tsl/windows/RC/0903/000366.mp4"
    # maxBox = [100, 130]
    # minBox = [50, 70]
    # loc = [840, 300, 840, 380]

    # videoPath = "/media/tsl/windows/RC/0903/000401.mp4"
    # maxBox = [100, 130]
    # minBox = [50, 70]
    # loc = [15, 200, 500, 500]

    # videoPath = "/media/tsl/windows/RC/0903/000402.mp4"
    # maxBox = [100, 130]
    # minBox = [50, 70]
    # loc = [15, 200, 500, 500]

    # videoPath = "/media/tsl/windows/RC/0903/000404.mp4"
    # maxBox = [100, 130]
    # minBox = [50, 70]
    # loc = [10, 300, 850, 620]

    # videoPath = "/media/tsl/windows/RC/0903/000419.mp4"
    # maxBox = [150, 200]
    # minBox = [70, 100]
    # loc = [230, 400, 1040, 660]

    # videoPath = "/media/tsl/windows/RC/0903/000426.mp4"
    # maxBox = [150, 200]
    # minBox = [70, 100]
    # loc = [230, 430, 890, 630]

    # videoPath = "/media/tsl/windows/RC/0903/000427.mp4"
    # maxBox = [150, 200]
    # minBox = [70, 100]
    # loc = [820, 330, 890, 630]

    # videoPath = "/media/tsl/windows/RC/0903/000428.mp4"
    # maxBox = [150, 200]
    # minBox = [70, 100]
    # loc = [230, 430, 890, 630]

    # videoPath = "/media/tsl/windows/RC/0903/000470.mp4"
    # maxBox = [60, 90]
    # minBox = [40, 50]
    # loc = [80, 180, 450, 300]

    # videoPath = "/media/tsl/windows/RC/小田视屏/26020115昌州二期中华奥城二号门对面花台内_云存储_1570614921_9B46C0E3/26020115昌州二期中华奥城二号门对面花台内_云存储_41D40598_1570614924_1.mp4"
    # maxBox = [90, 150]
    # minBox = [60, 90]
    # loc = [1090, 180, 800, 550]

    # videoPath = "/media/tsl/windows/RC/小田视屏/26020132昌州仁和一路83号附1号侧面17联通_云存储_1570620041_4066BCE9/26020132昌州仁和一路83号附1号侧面17联通_云存储_4F6C6180_1570620042_1.mp4"
    # maxBox = [80, 110]
    # minBox = [40, 60]
    # loc = [170, 190, 680, 400]

    # videoPath = "/media/tsl/windows/RC/视频1015/国泰路22号附22号右枪机_1571135615/国泰路22号附22号右枪机_0650F120_1571135699_1.mp4"
    # maxBox = [90, 120]
    # minBox = [60, 80]
    # loc = [50, 120, 680, 420]

    # videoPath = "/media/tsl/windows/RC/视频1015/国泰路22号附60号右枪机_1571135945/国泰路22号附60号右枪机_066B9DF0_1571135947_1.mp4"
    # maxBox = [150, 200]
    # minBox = [60, 80]
    # loc = [250, 70, 800, 1000]

    # videoPath = "/media/tsl/windows/RC/视频1015/国泰路22号附78号枪机_1571136516/国泰路22号附78号枪机_066B9DF0_1571136657_1.mp4"
    # maxBox = [150, 200]
    # minBox = [60, 80]
    # loc = [250, 70, 800, 1000]

    # videoPath = "/media/tsl/windows/RC/视频1015/国泰路22号附78号枪机_1571136565/国泰路22号附78号枪机_1C657840_1571136686_1.mp4"
    # maxBox = [150, 220]
    # minBox = [60, 80]
    # loc = [250, 70, 800, 1000]

    # videoPath = "/media/tsl/windows/RC/视频1015/国泰路22号附99号枪机_1571136881/国泰路22号附99号枪机_066B9DF0_1571136884_1.mp4"
    # maxBox = [150, 220]
    # minBox = [60, 80]
    # loc = [300, 100, 800, 1000]

    # videoPath = "/media/tsl/windows/RC/视频1015/国泰路22号附22号右枪机_1571135794/国泰路22号附22号右枪机_066B9DF0_1571135830_1.mp4"
    # maxBox = [90, 120]
    # minBox = [60, 80]
    # loc = [50, 120, 680, 420]

    # videoPath = "/media/tsl/windows/RC/视频1015/廉租房西正大门球机_1571138372/廉租房西正大门球机_0402AB30_1571138374_1.mp4"
    # maxBox = [130, 200]
    # minBox = [70, 90]
    # loc = [10, 270, 900, 800]

    # videoPath = "/media/tsl/windows/RC/视频1015/廉租房西正大门球机_1571138413/廉租房西正大门球机_1C3DE9B8_1571138415_1.mp4"
    # maxBox = [130, 200]
    # minBox = [70, 90]
    # loc = [10, 270, 900, 800]

    # videoPath = "/media/tsl/windows/RC/视频1015/廉租房西正大门球机_1571136125/廉租房西正大门球机_0670BE20_1571136129_1.mp4"
    # maxBox = [130, 200]
    # minBox = [70, 90]
    # loc = [700, 750, 1000, 700]

    videoName = videoPath.split("/")[-1].split(".")[0]
    cap = cv2.VideoCapture(videoPath)
    ret, frame = cap.read()

    fps = int(cap.get(cv2.CAP_PROP_FPS))
    fourcc = cv2.VideoWriter_fourcc(*"MJPG")
    # iw, ih = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    # x,y,w,h = loc
    fpsNum = 0
    labelStart = 0
    timeDistance = 1

    fileList = []
    labelImgPaths = ["/mnt/data/Pyproject/rubbish1030/videoFPS1031-2kerasAgutation/"]
    # labels = "labels1022"
    labelsSaveDst = "/mnt/data/Pyproject/rubbish1030/videoFPS1031-2TrainingData"
    maxBox = [200, 200]
    minBox = [60, 60]

    for labelImgPath in labelImgPaths:
        for file in os.listdir(labelImgPath):
            if (not file.endswith(".txt")):
                img = cv2.imread(os.path.join(labelImgPath, file))
                fileList.append(img)

    labelTotal = len(fileList)

    random.shuffle(fileList)
    random.shuffle(fileList)
    random.shuffle(fileList)
    needRemoveLoc = []

    backPath = "/mnt/data/Pyproject/videosFPS"
    backFiles = os.listdir(backPath)
    random.shuffle(backFiles)
    random.shuffle(backFiles)
    random.shuffle(backFiles)
    random.shuffle(backFiles)
    random.shuffle(backFiles)

    stopNUm = 0
    # while(ret):
    for file in backFiles:
        stopNUm += 1
        # if(stopNUm>100):
        #     break
        print("num=============================================================", stopNUm)
        videoName = file.split(".")[0]
        locRoi = []
        frame = cv2.imread(os.path.join(backPath, file))
        _, w, h = frame.shape[::-1]
        # ret,frame = cap.read()
        # cv2.imwrite("frame.jpg",frame)
        # break
        if (frame is None):
            ret, frame = cap.read()
            if (frame is None):
                print("img is None")
                break

        # if(fpsNum % (fps*timeDistance) == 0):
        if (True):
            backRoi = frame  # frame[slice(y, y + h), slice(x, x + w)]
            # backShow = copy.copy(backRoi)
            with open(labelsSaveDst + "/" + videoName + str(fpsNum) + ".txt", 'w') as f:
                locMsg = []

                for i in range(vl.randInt(1, 2)):
                    try:
                        labelRoi = fileList[labelStart]
                    except:
                        labelStart = 0
                        continue

                    _, lw, lh = labelRoi.shape[::-1]
                    # backRoi = frame[slice(y,y+h),slice(x,x+w)]
                    # while(w-lw<0 or h-lh<0):
                    # while(2*w-lw<0 or 2*h-lh<0):
                    # while(True):
                    labelStart += 1
                    print(labelStart)
                    try:
                        labelRoi = fileList[labelStart]
                    except:
                        labelStart = 0
                        continue

                    _, lw, lh = labelRoi.shape[::-1]

                    # if (w > 0.7*lw or h > 0.7*lh):
                    #     lh2 = vl.randInt(0.3 * h, 0.7 * h)
                    #     labelRoi = cv2.resize(labelRoi, (  lw ,lh2)   )
                    #     _, lw, lh = labelRoi.shape[::-1]

                    # if(lw>0.4*w):
                    #     lw2 = vl.randInt(int(0.3*lw), int(0.7*lw) )
                    #     labelRoi = cv2.resize(labelRoi, (int(lw2), lh))
                    #     _, lw, lh = labelRoi.shape[::-1]

                    # if(w<lw):
                    #     lw = vl.randInt(0.3 * w, 0.7 * lh)
                    #     labelRoi = cv2.resize(labelRoi,(lw,lh))
                    #     _, lw, lh = labelRoi.shape[::-1]

                    try:

                        # startX = vl.randInt(vl.randInt(loc[2]/15,loc[2]/13), w - lw)
                        # startY = vl.randInt(vl.randInt(loc[2]/15,loc[2]/13), h - lh)

                        startX = vl.randInt(vl.randInt(15, 50), w - lw)
                        startY = vl.randInt(vl.randInt(15, 50), h - lh)

                    except:
                        print("error")

                        continue

                    # if(lw>0.7*w or lh>0.7*h):
                    #     startX = vl.randInt(0, w - lw)
                    #     startY = vl.randInt(0, h - lh)
                    #     lw2 = vl.randInt(0.3*w,0.7*w)
                    #     labelRoi = cv2.resize(labelRoi,(lw2,lw2/(1.0*lw)*lh))

                    _, lw, lh = labelRoi.shape[::-1]
                    ifResize = False
                    if (lw > maxBox[0] or lw < minBox[0]):
                        lw = vl.randInt(minBox[0], maxBox[0])
                        ifResize = True
                    if (lh > maxBox[1] or lh < minBox[1]):
                        lh = vl.randInt(minBox[1], maxBox[1])
                        ifResize = True
                    if ifResize:
                        labelRoi = cv2.resize(labelRoi, (lw, lh))
                        _, lw, lh = labelRoi.shape[::-1]

                    labelStart += 1
                    if (labelStart > labelTotal):
                        labelStart = 0

                    try:
                        backRoi[slice(startY, startY + lh), slice(startX, startX + lw)] = labelRoi
                        backRoiResult = copy.copy(backRoi)
                        tmp = copy.copy(backRoi)
                        print("labelRoi:", lw, lh)
                        # cv2.rectangle(tmp,(startX,startY),(startX+int(lw),startY+int(lh) ),(0,0,255),8,2 )
                        locRoi.append([startX, startY, int(lw), int(lh)])

                        rx, ry, rw, rh = vl.convert([w, h], [startX, startX + lw, startY, startY + lh])
                        locMsg.append([rx, ry, rw, rh])
                        # with open(labelsSaveDst+"/"+videoName+str(fpsNum)+".txt",'a') as f:


                    except:
                        continue

                if (len(locMsg) != 0):

                    for i in locMsg:
                        labelResult = "0 " + " ".join(list(map(str, i))) + "\n"
                        print(labelResult)
                        f.write(labelResult)
                else:
                    needRemoveLoc.append(labelsSaveDst + "/" + videoName + str(fpsNum) + ".txt")

            # with open(labels + "/" + videoName + str(fpsNum) + ".txt", 'a') as f:
            #     rx, ry, rw, rh = vl.convert([w, h], [460-loc[0],460-loc[0]+45, 180-loc[0] , 180-loc[0] + 70]  )
            #     labelResult = "0 " + " ".join(list(map(str,[rx, ry, rw, rh]))) + "\n"
            #     f.write(labelResult)

            for i in locRoi:
                bx, by, bw, bh = i
                cv2.rectangle(tmp, (bx, by), (bx + bw, by + bh), (255, 0, 0), 2, 1)

            cv2.imwrite(labelsSaveDst + "/" + videoName + str(fpsNum) + ".jpg", backRoiResult)
            # cv2.imshow("label", tmp)
            tmpShow = cv2.resize(tmp, (960, 540))
            cv2.imshow("label", tmpShow)
            cv2.waitKey(1)

        # break
        fpsNum += 1

    for i in needRemoveLoc:
        os.system("rm " + i.replace("txt", "*"))
        print(i, " has removed.")

def imgLabel(src, dst="", labelPaths=""):
    labelImgsPath = []
    labelImgs = []
    timeLabel = time.strftime("%Y" + "%m" + "%d")

    minSize = [100, 250]
    maxSize = [100, 250]
    maxW, maxH = 600, 400
    minW, minH = 180, 180

    limit = 100

    ii = 0
    for root, dires, files in os.walk(labelPaths):
        for dire in dires:
            for file in os.listdir(os.path.join(root, dire)):
                labelImgsPath.append(os.path.join(os.path.join(root, dire), file))
                img = cv2.imread(os.path.join(os.path.join(root, dire), file))
                labelImgs.append(img)
                print(file)
                ii += 1
                # if(ii>50):
                #     break
        if (len(dires) == 0):
            for file in files:
                img = cv2.imread(os.path.join(labelPaths, file))
                labelImgs.append(img)
                print(file)
                ii += 1

    print("loaded all labelImg")
    total = len(labelImgs)

    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)

    backImgFiles = os.listdir(src)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    num = 0
    imgNum = 0
    needRemove = []

    for backImgFile in backImgFiles:
        imgNum += 1
        if (imgNum > 8000):
            break

        backImg = cv2.imread(os.path.join(src, backImgFile))
        _, bw, bh = backImg.shape[::-1]
        backImgResize = backImg  # cv2.resize(backImg, (vl.randInt(bw*0.7,bw*1.1),vl.randInt(bh*0.7,bh*1.1)))
        back = copy.copy(backImgResize)
        _, bw, bh = backImgResize.shape[::-1]
        with open(os.path.join(dst, backImgFile.split(".")[0] + ".txt"), 'w') as f:
            ratW, ratH = vl.randInt(4, 6) / 4.0, vl.randInt(4, 6) / 4.0
            for i in range(vl.randInt(1, 2)):
                # for i in range(1,2):

                if (num < total):
                    labelImg = labelImgs[num]
                    if (i == 1):
                        labelImg = labelImgs[vl.randInt(0, total / 2)]

                else:
                    num = 0
                    labelImg = labelImgs[num]
                    if (i == 1):
                        labelImg = labelImgs[vl.randInt(0, total / 2)]

                # labelImgResize = labelImg
                _, iw, ih = labelImg.shape[::-1]
                if (iw < minW or ih < minH):
                    if (iw < minW):
                        iwNew = vl.randInt(minW, maxW)
                        ihNew = int(min(ih, iwNew / iw * ih))
                        labelImgResize = cv2.resize(labelImg, (iwNew, ihNew))
                        _, iw, ih = labelImgResize.shape[::-1]
                    elif (ih < minH):
                        ihNew = vl.randInt(minH, maxH)
                        iwNew = int(min(iw, ihNew / ih * ihNew))
                        labelImgResize = cv2.resize(labelImg, (iwNew, ihNew))
                        _, iw, ih = labelImgResize.shape[::-1]
                else:
                    labelImgResize = cv2.resize(labelImg, (int(iw * ratW), int(ih * ratH)))
                _, lw, lh = labelImgResize.shape[::-1]

                if (lw < limit or lh < limit):
                    try:
                        print("adfasd")
                        # os.remove(os.path.join(dst, backImgFile.split(".")[0] + ".txt"))
                        # os.remove(os.path.join(dst, backImgFile.split(".")[0] + ".jpg"))
                    except:
                        # needRemove.append(os.path.join(dst, backImgFile.split(".")[0]))
                        continue
                    # needRemove.append(os.path.join(dst, backImgFile.split(".")[0]))
                    continue

                # if(lw>maxW):
                #     labelImgResize = cv2.resize(labelImg, (vl.randInt(maxW-100,maxW),lh))
                # if(lh>maxH):
                #     labelImgResize = cv2.resize(labelImg, (lw,vl.randInt(maxH-100,maxH)))
                # if(lw<minW):
                #     labelImgResize = cv2.resize(labelImg, (vl.randInt(minW,minW+100),lh))
                # if (lh < minH):
                #     labelImgResize = cv2.resize(labelImg, (lw, vl.randInt(minH,minH + 100)))

                # labelImgResize = labelImg
                # labelImgResize = cv2.resize(labelImg,(vl.randInt(minSize[0],maxSize[0]),vl.randInt(maxSize[0],maxSize[1])))

                _, lw, lh = labelImgResize.shape[::-1]
                try:
                    startX = vl.randInt(vl.randInt(15, 50), bw - lw)
                    startY = vl.randInt(vl.randInt(15, 50), bh - lh)
                except:
                    try:
                        print("adfad")
                        # os.remove(os.path.join(dst, backImgFile.split(".")[0] + ".txt"))
                        # os.remove(os.path.join(dst, backImgFile.split(".")[0] + ".jpg"))
                    except:
                        continue
                    continue
                backImgResize[slice(startY, startY + lh), slice(startX, startX + lw)] = labelImgResize
                back[slice(startY, startY + lh), slice(startX, startX + lw)] = labelImgResize
                x, y, w, h = startX, startY, lw, lh
                rx, ry, rw, rh = vl.convert([bw, bh], [x, x + w, y, y + h])
                labelMsg = " ".join(map(str, [0, rx, ry, rw, rh])) + "\n"
                print(x, y, w, h)
                f.write(labelMsg)
                cv2.rectangle(back, (x, y), (x + w, y + h), (0, 0, 255), 2, 2)

        if (os.path.isfile(os.path.join(dst, backImgFile.split(".")[0] + ".txt"))):
            with open(os.path.join(dst, backImgFile.split(".")[0] + ".txt")) as f:
                lines = f.readlines()
                if (len(lines) == 0):
                    os.remove(os.path.join(dst, backImgFile.split(".")[0] + ".txt"))
                else:
                    imgShow = cv2.resize(back, (960, 540))
                    cv2.imshow("img", imgShow)
                    cv2.imwrite(dst + "/" + backImgFile.split(".")[0] + ".jpg", backImgResize)
                    cv2.waitKey(1)
        num += 1

def nms(locs):
    if(len(locs)==1):
        return 0
    
    _,x1,y1,w1,h1 = locs[0]
    _,x2,y2,w2,h2 = locs[1]

    rx1 = max(x1,x2)
    ry1 = max(y1,y2)
    rx2 = min(x1+w1,x2+w2)
    ry2 = min(y1+h1,y2+h2)

    x = rx1
    y = ry2
    w = max(0,rx2 - rx1)
    h = max(0,ry2 - ry1)

    roiSize = w*h
    # iou = roiSize/(w1*h1 + w2*h2 - roiSize)
    iou = roiSize/min(w1*h1,w2*h2)
    return iou

def imgLabelHoldOnLabelSize(src, dst="", labelPaths=""):
    labelImgsPath = []
    labelImgs = []
    timeLabel = time.strftime("%Y" + "%m" + "%d")

    minSize = [100, 250]
    maxSize = [100, 250]
    maxW, maxH = 700, 400
    minW, minH = 300, 180

    limit = 100

    ii = 0
    for root, dires, files in os.walk(labelPaths):
        for dire in dires:
            for file in os.listdir(os.path.join(root, dire)):
                labelImgsPath.append(os.path.join(os.path.join(root, dire), file))
                img = cv2.imread(os.path.join(os.path.join(root, dire), file))
                if(img is None):
                    print("adfasd")
                labelImgs.append([img,dire])
                print(file)
                ii += 1

        # if (len(dires) == 0):
        #     for file in files:
        #         img = cv2.imread(os.path.join(labelPaths, file ))
        #         if (img is None):
        #             print("adfad")
        #         labelImgs.append(img)
        #
        #         print(file)
        #         ii += 1
    # return
    print("loaded all labelImg")
    total = len(labelImgs)

    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)

    backImgFiles = os.listdir(src)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    num = 0
    imgNum = 0

    for backImgFile in backImgFiles:
        imgNum += 1
        # if (imgNum > 8000):
        #     break

        backImg = cv2.imread(os.path.join(src, backImgFile))
        _, bw, bh = backImg.shape[::-1]
        backImgResize = backImg  # cv2.resize(backImg, (vl.randInt(bw*0.7,bw*1.1),vl.randInt(bh*0.7,bh*1.1)))
        back = copy.copy(backImgResize)
        _, bw, bh = backImgResize.shape[::-1]
        with open(os.path.join(dst, backImgFile.split(".")[0] + ".txt"), 'w') as f:
            ratW, ratH = 1,1#vl.randInt(3, 5) / 4.0, vl.randInt(3, 5) / 4.0
            locs = []
            for i in range(vl.randInt(1, 2)):
                if (num < total):
                    labelImg,labelSave = labelImgs[num]
                    if (i == 1):
                        labelImg,labelSave = labelImgs[vl.randInt(0, total / 2)]

                else:
                    num = 0
                    labelImg,labelSave = labelImgs[num]
                    if (i == 1):
                        labelImg,labelSave = labelImgs[vl.randInt(0, total / 2)]

                _, iw, ih = labelImg.shape[::-1]
                labelImgResize = cv2.resize(labelImg, (int(iw * ratW), int(ih * ratH)))

                if (iw < minW or ih < minH):

                    if (iw < minW):
                        iwNew = vl.randInt(minW, maxW)
                        ihNew = int(min(ih, iwNew / iw * ih))
                        labelImgResize = cv2.resize(labelImg, (iwNew, ihNew))
                        _, iw, ih = labelImgResize.shape[::-1]

                    if (ih < minH):
                        ihNew = vl.randInt(minH, maxH)
                        iwNew = int(min(iw, ihNew / ih * ihNew))
                        labelImgResize = cv2.resize(labelImg, (iwNew, ihNew))
                        _, iw, ih = labelImgResize.shape[::-1]
                else:
                    labelImgResize = cv2.resize(labelImg, (int(iw * ratW), int(ih * ratH)))
                _, lw, lh = labelImgResize.shape[::-1]

                # if (lw < limit or lh < limit):
                #     pass

                # if(lw>maxW):
                #     labelImgResize = cv2.resize(labelImg, (vl.randInt(maxW-100,maxW),lh))
                # if(lh>maxH):
                #     labelImgResize = cv2.resize(labelImg, (lw,vl.randInt(maxH-100,maxH)))
                # if(lw<minW):
                #     labelImgResize = cv2.resize(labelImg, (vl.randInt(minW,minW+100),lh))
                # if (lh < minH):
                #     labelImgResize = cv2.resize(labelImg, (lw, vl.randInt(minH,minH + 100)))

                # labelImgResize = labelImg
                # labelImgResize = cv2.resize(labelImg,(vl.randInt(minSize[0],maxSize[0]),vl.randInt(maxSize[0],maxSize[1])))

                _, lw, lh = labelImgResize.shape[::-1]


                try:
                    startX = vl.randInt(vl.randInt(15, 50), bw - lw)
                    startY = vl.randInt(vl.randInt(15, 50), bh - lh)
                except:
                   startX = 50
                   startY = 50

                locs.append([startX,startY,lw, lh])
                iouValue = nms(locs)
                # print("IOU=",iouValue,backImgFile)
                if(iouValue>0.1):
                    # print("IOU>0.2===========================",iouValue,backImgFile)
                    continue
                backImgResize[slice(startY, startY + lh), slice(startX, startX + lw)] = labelImgResize
                back[slice(startY, startY + lh), slice(startX, startX + lw)] = labelImgResize
                x, y, w, h = startX, startY, lw, lh
                rx, ry, rw, rh = vl.convert([bw, bh], [x, x + w, y, y + h])
                labelMsg = " ".join(map(str, [str(labelSave), rx, ry, rw, rh])) + "\n"
                # print(x, y, w, h)
                f.write(labelMsg)
                cv2.rectangle(back, (x, y), (x + w, y + h), (0, 0, 255), 2, 2)

        if (os.path.isfile(os.path.join(dst, backImgFile.split(".")[0] + ".txt"))):
            with open(os.path.join(dst, backImgFile.split(".")[0] + ".txt")) as f:
                lines = f.readlines()
                if (len(lines) == 0):
                    os.remove(os.path.join(dst, backImgFile.split(".")[0] + ".txt"))
                else:
                    imgShow = cv2.resize(back, (960, 540))
                    cv2.imshow("img", imgShow)
                    cv2.imwrite(dst + "/" + backImgFile.split(".")[0] + ".jpg", backImgResize)
                    cv2.waitKey(1)
        num += 1

def imgLaLabelDire(src, dst="", labelPaths=""):
    labelImgsPath = []
    labelImgs = []
    timeLabel = time.strftime("%Y" + "%m" + "%d")

    ii = 0
    for root, dires, files in os.walk(labelPaths):
        for dire in dires:
            for file in os.listdir(os.path.join(root, dire)):
                if(file.endswith("txt")):
                    continue
                labelImgsPath.append(os.path.join(os.path.join(root, dire), file))
                img = cv2.imread(os.path.join(os.path.join(root, dire), file))
                if(img is None):
                    print("adfasd")
                labelImgs.append([img,dire])
                print(file)
                ii += 1

        # if (len(dires) == 0):
        #     for file in files:
        #         img = cv2.imread(os.path.join(labelPaths, file ))
        #         if (img is None):
        #             print("adfad")
        #         labelImgs.append(img)
        #
        #         print(file)
        #         ii += 1
    # return
    print("loaded all labelImg")
    total = len(labelImgs)

    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)

    backImgFiles = os.listdir(src)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    num = 0
    imgNum = 0

    for backImgFile in backImgFiles:
        imgNum += 1

        # if (imgNum > 8000):
        #     break
        labelID = 0
        try:
            backImg = cv2.imread(os.path.join(src, backImgFile))

        except:
            print(backImgFile)
        try:
            _, bw, bh = backImg.shape[::-1]
        except:
            print(backImgFile)
            continue
        backImgResize = backImg
        back = copy.copy(backImgResize)
        _, bw, bh = backImgResize.shape[::-1]
        with open(os.path.join(dst, backImgFile.split(".")[0] + ".txt"), 'w') as f:
            locs = []
            for i in range(vl.randInt(1, 2)):
                if (num < total):
                    labelImg,labelSave = labelImgs[num]
                    if (i == 1):
                        labelImg,labelSave = labelImgs[vl.randInt(0, total / 2)]

                else:
                    num = 0
                    labelImg,labelSave = labelImgs[num]
                    if (i == 1):
                        labelImg,labelSave = labelImgs[vl.randInt(0, total / 2)]

                _, iw, ih = labelImg.shape[::-1]
                labelImgResize = labelImg#cv2.resize(labelImg, (int(iw * ratW), int(ih * ratH)))

                # if (iw < minW or ih < minH):
                #
                #     if (iw < minW):
                #         iwNew = vl.randInt(minW, maxW)
                #         ihNew = int(min(ih, iwNew / iw * ih))
                #         labelImgResize = cv2.resize(labelImg, (iwNew, ihNew))
                #         _, iw, ih = labelImgResize.shape[::-1]
                #
                #     if (ih < minH):
                #         ihNew = vl.randInt(minH, maxH)
                #         iwNew = int(min(iw, ihNew / ih * ihNew))
                #         labelImgResize = cv2.resize(labelImg, (iwNew, ihNew))
                #         _, iw, ih = labelImgResize.shape[::-1]
                # else:
                #     labelImgResize = cv2.resize(labelImg, (int(iw * ratW), int(ih * ratH)))
                _, lw, lh = labelImgResize.shape[::-1]

                # if (lw < limit or lh < limit):
                #     pass

                # if(lw>maxW):
                #     labelImgResize = cv2.resize(labelImg, (vl.randInt(maxW-100,maxW),lh))
                # if(lh>maxH):
                #     labelImgResize = cv2.resize(labelImg, (lw,vl.randInt(maxH-100,maxH)))
                # if(lw<minW):
                #     labelImgResize = cv2.resize(labelImg, (vl.randInt(minW,minW+100),lh))
                # if (lh < minH):
                #     labelImgResize = cv2.resize(labelImg, (lw, vl.randInt(minH,minH + 100)))

                # labelImgResize = labelImg
                # labelImgResize = cv2.resize(labelImg,(vl.randInt(minSize[0],maxSize[0]),vl.randInt(maxSize[0],maxSize[1])))

                # _, lw, lh = labelImgResize.shape[::-1]


                try:
                    startX = vl.randInt(vl.randInt(15, 50), bw - lw)
                    startY = vl.randInt(vl.randInt(15, 50), bh - lh)
                except:
                   startX = 50
                   startY = 50

                locs.append([startX,startY,lw, lh])
                iouValue = nms(locs)
                # print("IOU=",iouValue,backImgFile)
                if(iouValue>0.1):
                    # print("IOU>0.1===========================",iouValue,backImgFile)
                    continue
                backImgResize[slice(startY, startY + lh), slice(startX, startX + lw)] = labelImgResize
                back[slice(startY, startY + lh), slice(startX, startX + lw)] = labelImgResize
                x, y, w, h = startX, startY, lw, lh
                rx, ry, rw, rh = vl.convert([bw, bh], [x, x + w, y, y + h])
                labelMsg = " ".join(map(str, [str(labelID), rx, ry, rw, rh])) + "\n"
                # print(x, y, w, h)
                f.write(labelMsg)
                cv2.rectangle(back, (x, y), (x + w, y + h), (0, 0, 255), 2, 2)

        if (os.path.isfile(os.path.join(dst, backImgFile.split(".")[0] + ".txt"))):
            with open(os.path.join(dst, backImgFile.split(".")[0] + ".txt")) as f:
                lines = f.readlines()
                if (len(lines) == 0):
                    os.remove(os.path.join(dst, backImgFile.split(".")[0] + ".txt"))
                else:
                    imgShow = cv2.resize(back, (960, 540))
                    cv2.imshow("img", imgShow)
                    cv2.imwrite(dst + "/" + backImgFile.split(".")[0] + ".jpg", backImgResize)
                    cv2.waitKey(1)
        num += 1


def readData(src):
    fileList = []
    for file in os.listdir(src):
        if ("_" not in file):
            if (file.endswith(".jpg")):
                videoName = "".join(list(file.split(".")[0])[:6])
                fileList.append(videoName)

    fileList.sort()
    for file in fileList:
        print(file)


def readFrame(src, dst):
    # args = sys.argv #"/mnt/data/videos/0903/000404.mp4"
    videoPath = "/mnt/data/videos/0903/000407.mp4"
    x1, y1, x2, y2 = 50, 50, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000408.mp4"
    x1, y1, x2, y2 = 50, 50, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000409.mp4"
    x1, y1, x2, y2 = 50, 50, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000410.mp4"
    x1, y1, x2, y2 = 50, 50, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000411.mp4"
    x1, y1, x2, y2 = 50, 50, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000412.mp4"
    x1, y1, x2, y2 = 50, 50, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000413.mp4"
    x1, y1, x2, y2 = 50, 50, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000414.mp4"
    x1, y1, x2, y2 = 50, 50, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000415.mp4"
    x1, y1, x2, y2 = 50, 50, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000416.mp4"
    x1, y1, x2, y2 = 50, 50, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000417.mp4"
    x1, y1, x2, y2 = 50, 50, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000418.mp4"
    x1, y1, x2, y2 = 50, 50, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000419.mp4"
    x1, y1, x2, y2 = 500, 380, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000420.mp4"
    x1, y1, x2, y2 = 50, 250, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000421.mp4"
    x1, y1, x2, y2 = 50, 50, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000422.mp4"
    x1, y1, x2, y2 = 50, 250, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000423.mp4"
    x1, y1, x2, y2 = 50, 250, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000424.mp4"
    x1, y1, x2, y2 = 50, 250, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000425.mp4"
    x1, y1, x2, y2 = 50, 250, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000426.mp4"
    x1, y1, x2, y2 = 300, 300, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000427.mp4"
    x1, y1, x2, y2 = 300, 300, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000428.mp4"
    x1, y1, x2, y2 = 300, 300, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000429.mp4"
    x1, y1, x2, y2 = 20, 30, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000430.mp4"
    x1, y1, x2, y2 = 20, 30, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000431.mp4"
    x1, y1, x2, y2 = 250, 250, 1800, 1070
    videoPath = "/mnt/data/videos/0903/000432.mp4"
    x1, y1, x2, y2 = 50, 250, 1800, 1070
    # videoPath = "/mnt/data/videos/0903/000433.mp4"
    # x1, y1, x2, y2 = 50, 250, 1800, 1070
    # videoPath = "/mnt/data/videos/0903/000436.mp4"
    # x1, y1, x2, y2 = 50, 250, 1800, 1070

    # videoPath = "/mnt/data/videos/0903/000434.mp4"
    # x1, y1, x2, y2 = 50, 250, 1800, 1070
    # videoPath = "/mnt/data/videos/0903/000435.mp4"
    # x1, y1, x2, y2 = 50, 250, 1800, 1070
    # videoPath = "/mnt/data/videos/0903/000437.mp4"
    # x1, y1, x2, y2 = 50, 250, 1800, 1070
    # videoPath = "/mnt/data/videos/0903/000438.mp4"
    # x1, y1, x2, y2 = 50, 250, 1800, 1070
    # videoPath = "/mnt/data/videos/0903/000439.mp4"
    # x1, y1, x2, y2 = 50, 250, 1800, 1070
    # videoPath = "/mnt/data/videos/0903/000440.mp4"
    # x1, y1, x2, y2 = 50, 250, 1800, 1070
    # videoPath = "/mnt/data/videos/0903/000441.mp4"
    # x1, y1, x2, y2 = 50, 250, 1800, 1070

    videoPath = "/mnt/data/videos/0903/000443.mp4"
    x1, y1, x2, y2 = 200, 30, 1400, 1070
    videoPath = "/mnt/data/videos/0903/000470.mp4"
    x1, y1, x2, y2 = 50, 200, 1400, 1070

    videoPath = "/mnt/data/videos/0903/000471.mp4"
    x1, y1, x2, y2 = 30, 250, 1900, 1070
    videoPath = "/mnt/data/videos/0903/000472.mp4"
    x1, y1, x2, y2 = 30, 250, 1900, 1070
    videoPath = "/mnt/data/videos/0903/000473.mp4"
    x1, y1, x2, y2 = 30, 250, 1900, 1070
    videoPath = "/mnt/data/videos/0903/000474.mp4"
    x1, y1, x2, y2 = 30, 250, 1900, 1070
    videoPath = "/mnt/data/videos/0903/000475.mp4"
    x1, y1, x2, y2 = 30, 250, 1900, 1070
    videoPath = "/mnt/data/videos/0903/000476.mp4"
    x1, y1, x2, y2 = 30, 250, 1900, 1070
    videoPath = "/mnt/data/videos/0903/000477.mp4"
    x1, y1, x2, y2 = 30, 250, 1900, 1070
    videoPath = "/mnt/data/videos/0903/000478.mp4"
    x1, y1, x2, y2 = 30, 250, 1900, 1070
    videoPath = "/mnt/data/videos/0903/000479.mp4"
    x1, y1, x2, y2 = 30, 250, 1900, 1070
    videoPath = "/mnt/data/videos/0903/000480.mp4"
    x1, y1, x2, y2 = 30, 250, 1900, 1070
    videoPath = "/mnt/data/videos/0903/000481.mp4"
    x1, y1, x2, y2 = 30, 250, 1900, 1070

    videoPath = "/mnt/data/videos/视频1015/国泰路22号枪机_1571138241/国泰路22号枪机_1C4A1CD8_1571138244_1.mp4"
    x1, y1, x2, y2 = 600, 230, 1900, 680
    videoPath = "/mnt/data/videos/视频1015/国泰路22号球机_1571137725/国泰路22号球机_1C4888E8_1571137727_1.mp4"
    x1, y1, x2, y2 = 60, 460, 2500, 1400
    # videoPath = "/mnt/data/videos/视频1015/国泰路22号球机_1571137846/国泰路22号球机_04028E48_1571137848_1.mp4"
    # x1, y1, x2, y2 = 20, 160, 2500, 600
    # videoPath = "/mnt/data/videos/视频1015/国泰路22号附22号右枪机_1571135615/国泰路22号附22号右枪机_0650F120_1571135699_1.mp4"
    # x1, y1, x2, y2 = 60, 260, 1160, 1060
    # videoPath = "/mnt/data/videos/视频1015/国泰路22号附22号右枪机_1571135720/国泰路22号附22号右枪机_0670BE20_1571135749_1.mp4"
    # x1, y1, x2, y2 = 60, 260, 1160, 1060
    # videoPath = "/mnt/data/videos/视频1015/国泰路22号附22号右枪机_1571135794/国泰路22号附22号右枪机_066B9DF0_1571135830_1.mp4"
    # x1, y1, x2, y2 = 60, 190, 670, 970
    # videoPath = "/mnt/data/videos/视频1015/国泰路22号附59号枪机_1571137464/国泰路22号附59号枪机_1C49EE98_1571137467_1.mp4"
    # x1, y1, x2, y2 = 600, 170, 1770, 1070
    # videoPath = "/mnt/data/videos/视频1015/国泰路22号附59号枪机_1571137479/国泰路22号附59号枪机_1C578810_1571137481_1.mp4"
    # x1, y1, x2, y2 = 600, 170, 1770, 1070
    # videoPath = "/mnt/data/videos/视频1015/国泰路22号附59号枪机_1571137486/国泰路22号附59号枪机_1C578DD8_1571137488_1.mp4"
    # x1, y1, x2, y2 = 600, 170, 1770, 1070
    # videoPath = "/mnt/data/videos/视频1015/国泰路22号附60号右枪机_1571135945/国泰路22号附60号右枪机_066B9DF0_1571135947_1.mp4"
    # x1, y1, x2, y2 = 300, 180, 1600, 1050
    videoPath = "/mnt/data/videos/视频1015/国泰路22号附61号枪机_1571137029/国泰路22号附61号枪机_066B9DF0_1571137031_1.mp4"
    x1, y1, x2, y2 = 200, 180, 1700, 870
    videoPath = "/mnt/data/videos/视频1015/国泰路22号附61号枪机_1571137062/国泰路22号附61号枪机_066B9DF0_1571137064_1.mp4"
    x1, y1, x2, y2 = 200, 180, 1700, 870
    # videoPath = "/mnt/data/videos/视频1015/国泰路22号附61号枪机_1571137101/国泰路22号附61号枪机_065F8348_1571137104_1.mp4"
    # x1, y1, x2, y2 = 200, 180, 1700, 870
    videoPath = "/mnt/data/videos/视频1015/国泰路22号附61号枪机_1571137120/国泰路22号附61号枪机_065F8348_1571137122_1.mp4"
    x1, y1, x2, y2 = 200, 180, 1700, 870
    videoPath = "/mnt/data/videos/视频1015/国泰路22号附61号枪机_1571137140/国泰路22号附61号枪机_03FCE1D8_1571137142_1.mp4"
    x1, y1, x2, y2 = 200, 180, 1700, 870
    videoPath = "/mnt/data/videos/视频1015/国泰路22号附61号枪机_1571137251/国泰路22号附61号枪机_065F8348_1571137253_1.mp4"
    x1, y1, x2, y2 = 200, 180, 1700, 870
    videoPath = "/mnt/data/videos/视频1015/国泰路22号附69号球机_1571138131/国泰路22号附69号球机_1C572238_1571138133_1.mp4"
    x1, y1, x2, y2 = 250, 600, 1200, 900
    videoPath = "/mnt/data/videos/视频1015/国泰路22号附78号枪机_1571136516/国泰路22号附78号枪机_066B9DF0_1571136657_1.mp4"
    x1, y1, x2, y2 = 200, 160, 1370, 1060
    videoPath = "/mnt/data/videos/视频1015/国泰路22号附78号枪机_1571136603/国泰路22号附78号枪机_066B9DF0_1571136708_1.mp4"
    x1, y1, x2, y2 = 200, 160, 1370, 1060
    videoPath = "/mnt/data/videos/视频1015/国泰路22号附78号枪机_1571136643/国泰路22号附78号枪机_03FCE1D8_1571136722_1.mp4"
    x1, y1, x2, y2 = 200, 160, 1370, 1060
    videoPath = "/mnt/data/videos/视频1015/国泰路22号附99号枪机_1571136914/国泰路22号附99号枪机_066B9DF0_1571136917_1.mp4"
    x1, y1, x2, y2 = 300, 160, 1770, 1060
    videoPath = "/mnt/data/videos/视频1015/国泰路22号附99号枪机_1571136900/国泰路22号附99号枪机_066B9DF0_1571136903_1.mp4"
    x1, y1, x2, y2 = 300, 160, 1770, 1060
    videoPath = "/mnt/data/videos/视频1015/廉租房西正大门出入口内_1571138453/廉租房西正大门出入口内_1C575078_1571138455_1.mp4"
    x1, y1, x2, y2 = 580, 100, 1850, 1060
    videoPath = "/mnt/data/videos/视频1015/廉租房西正大门出入口内_1571139384/廉租房西正大门出入口内_06654938_1571139386_1.mp4"
    x1, y1, x2, y2 = 580, 100, 1850, 1060
    videoPath = "/mnt/data/videos/视频1015/廉租房西正大门出入口内_1571139406/廉租房西正大门出入口内_06652688_1571139408_1.mp4"
    x1, y1, x2, y2 = 580, 100, 1850, 1060
    videoPath = "/mnt/data/videos/视频1015/廉租房西正大门球机_1571136074/廉租房西正大门球机_066B9DF0_1571136076_1.mp4"
    x1, y1, x2, y2 = 650, 720, 2030, 1400
    videoPath = "/mnt/data/videos/视频1015/廉租房西正大门球机_1571136125/廉租房西正大门球机_0670BE20_1571136129_1.mp4"
    x1, y1, x2, y2 = 650, 720, 2030, 1400
    videoPath = "/mnt/data/videos/视频1015/廉租房西正大门球机_1571136157/廉租房西正大门球机_066B9DF0_1571136258_1.mp4"
    x1, y1, x2, y2 = 650, 720, 2030, 1400

    # args = sys.argv
    # videoPath = args[1]
    # dst = "../videoJPG"
    print(videoPath)
    distance = 4
    ix, iy, iw, ih = x1, y1, x2 - x1, y2 - y1
    for video in os.listdir(src):
        videoPath = os.path.join(src, video)
        cap = cv2.VideoCapture(videoPath)
        fps, fourcc, [w, h] = vl.videoMsg(cap)
        ret, frame = cap.read()
        videoName = videoPath.split(".")[0].split("/")[-1]
        num = 0

        while (ret):
            if (num == 0):
                cv2.imwrite("frame.jpg", frame)
            # break
            ret, frame = cap.read()
            if (num % fps) == 0:
                if (frame is not None):
                    # img = frame[slice(iy,iy+ih),slice(ix,ix+iw)]
                    img = frame  # [slice(iy,iy+ih),slice(ix,ix+iw)]
                    cv2.imshow("img", img)
                    cv2.waitKey(1)
                    cv2.imwrite(os.path.join(dst, videoName + str(int(num / (fps * distance))).zfill(6)) + ".jpg", img)
                    print(int(num / (fps * distance)), videoName)
            num += 1

def readFPSFromVideos():
    videoPath = "/mnt/data/1104/"
    videoList = []
    for video in os.listdir(videoPath):
        if ("国" in video and (not video.startswith("_"))):
            videoList.append(video)
        if ("12_荣昌大道97号球机_1572944269" in video):
            videoList.append(video)

    videoList = sorted(videoList, key=lambda b: int(b.split("_")[0]))

    for num, video in enumerate(videoList):
        print(video)
        name = int(video.split("_")[0])
        if (name != 11):
            continue
        # continue
        srcPath = os.path.join(videoPath, video)
        for src in os.listdir(srcPath):
            src = os.path.join(srcPath, src)
            # src = "/mnt/data/1104/国泰路22号枪机_1572925592/国泰路22号枪机_056A79E0_1572925594_1.mp4"
            dst = "/mnt/data/1104/videoFPS/" + src.split("/")[-1].split(".")[0] + "FPS" + str(num + 1).zfill(3)
            mkpath(dst)
            vl.readFPS(src, dst)

def tmp():
    color = [(0, 0, 255), (0, 255, 255), (0, 255, 0)]
    src = "/media/tsl/videos/ships/boat/"
    for file in os.listdir(src):
        if (file.endswith(".jpg")):
            img = cv2.imread(os.path.join(src, file))
            _, wi, hi = img.shape[::-1]
            size = [wi, hi]
            txtFile = file.replace("jpg", "txt")
            with open(os.path.join(src, txtFile), "r") as f:
                lines = f.readlines()
                for line in lines:
                    label, rx, ry, rw, rh = list(map(float, line.rstrip().split(" ")))
                    x, y, w, h = vl.convertR(rx, ry, rw, rh, wi, hi)
                    x, y, w, h = list(map(int, [x, y, w, h]))
                    cv2.rectangle(img, (x, y), (x + w, y + h), color[int(label)], 2, 2)
            cv2.imshow("img", img)
            cv2.imwrite("/media/tsl/videos/ships/tmp/" + file, img)
            # cv2.waitKey(1)

def removeSameLabel(src, dst="dst"):
    for file in os.listdir(src):
        if (file.endswith("txt") and (not file.startswith("cl")) and (not file.startswith("la"))):
            with open(os.path.join(src, file), 'r') as f:
                target = open(os.path.join(dst, file), "w")
                lines = f.readlines()
                # set(line)
                lines = list(set(lines))
                print(len(lines), file)

                linesWR = "".join(lines)
                target.write(linesWR)
                target.close()

def fps2video(src, src2, dst):
    fourcc = cv2.VideoWriter_fourcc(*"MJPG")
    frame = cv2.imread(os.path.join(src, os.listdir(src)[0]))
    _, w, h = frame.shape[::-1]
    fps = 15
    writer = cv2.VideoWriter(os.path.join(dst, "result.avi"), fourcc, fps, (w, h))

    list2_ = os.listdir(src2)
    list2 = []
    for num, f in enumerate(list2_):
        if (f.endswith(".jpg")):
            list2.append(f)
            print(num, f)

    list2 = [os.path.join(src2, i + ".jpg") for i in
             sorted([i.split(".")[0] for i in list2], key=lambda b: int("".join(b.split("_"))))]

    list1_ = [os.path.join(src, i) for i in sorted(os.listdir(src))]
    list1 = []
    for i in list1_:
        if (i.endswith(".jpg")):
            list1.append(i)

    list2.extend(list1)

    for n, file in enumerate(list2):
        if (file.endswith("jpg")):
            framePath = os.path.join(src, file)
            frame = cv2.imread(framePath)
            cv2.imshow("frame", frame)
            cv2.waitKey(1)
            writer.write(frame)
            print(n)

def removeImgByLabel(src, dst):
    for txtFile in os.listdir(src):
        if(txtFile.endswith(".txt") and not txtFile.startswith("cla")):
            with open(os.path.join(src,txtFile),'r') as f:
                lines = f.readlines()
                if(len(lines)<1):
                    shutil.move(os.path.join(src,txtFile),dst)
                    print (txtFile,"has moved to",dst)

    for file in os.listdir(src):
        if (not file.endswith(".txt") and (not file.startswith("cl"))):
            txtFile = file.split(".")[0] + ".txt"
            if (not os.path.isfile(os.path.join(src, txtFile))):
                needRemove = os.path.join(src, file)
                try:
                    shutil.move(needRemove, dst)
                except:
                    print("asdfasdfasd")
                    # os.system("mv " +needRemove + " " + dst + "/")
                    # os.remove(needRemove)

                print(file, "has moved to",dst)

def checkLabelSlice(src,dst):
    for txtFile in os.listdir(src):
        if(txtFile.endswith(".txt") and not txtFile.startswith("cl")):
            print(txtFile)
            name = txtFile.split(".")[0]
            img = cv2.imread(os.path.join(src,txtFile.replace("txt","jpg")))
            _,iw,ih = img.shape[::-1]
            with open(os.path.join(src,txtFile),"r") as f:
                lines = f.readlines()
                for num,line in enumerate(lines):
                    label,rx,ry,rw,rh = list(map(float,(line.rstrip().split(" "))))
                    x,y,w,h = vl.convertR( rx, ry, rw, rh, iw, ih)
                    x, y, w, h = list(map(int,[x,y,w,h]))
                    print(x,y,w,h)
                    roi = img[slice(y,y+h),slice(x,x+w)]
                    cv2.imwrite(os.path.join(os.path.join(dst,str(int(label))),name+"_"+str(num))+".jpg",roi)


def renameTrainData(src,dst):
    start = src.split("/")[-1]
    num = 0
    for file in os.listdir(src):
        if (not(file.startswith("Train") or(file.startswith("Val")) or file.startswith("cl"))):
            if(not file.endswith("txt")):
                txtFile = os.path.join(src,file.split(".")[0]+".txt")
                jpgFile = os.path.join(src,file)
                name = start+"_"+str(num).zfill(6)
                shutil.copy(txtFile,os.path.join(dst,name+".txt"))
                shutil.copy(jpgFile,os.path.join(dst,name+"."+file.split(".")[-1]))
                num+=1

def Here(imgPath):
    img = cv2.imread(imgPath)
    _,wi,hi = img.shape[::-1]
    with open(imgPath.replace("jpg","txt"),'r') as f:
        lines = f.readlines()
        for line in lines:
            label,rx, ry, rw, rh = line.rstrip().split(" ")
            rx, ry, rw, rh = list(map(float,[rx, ry, rw, rh]))
            # print(label)
            x,y,w,h = vl.convertR(rx, ry, rw, rh,wi,hi)
            print(x,y,w,h)

def removeNullImgLabel(labelImgPath):

    for txt in os.listdir(labelImgPath):
        if(txt.endswith(".txt")):
            txtPath = os.path.join(labelImgPath,txt)
            with open(txtPath,"r")as f:
                lines = f.readlines()
                if(len(lines)==0):
                    os.remove(txtPath)

    for img in os.listdir(labelImgPath):
        if img.endswith(".jpg") or img.endswith(".png"):
            imgPath = os.path.join(labelImgPath,img)
            txtPath = os.path.join(labelImgPath,img.split(".")[0]+".txt")
            if( not (os.path.exists(imgPath) and os.path.exists(txtPath)) ):
                try:
                    os.remove(imgPath)
                except:
                    print("no such file",imgPath)
                try:
                    os.remove(txtPath)
                except:
                    print("no such file",txtPath)


    for txt in os.listdir(labelImgPath):
        if(txt.endswith("txt")):
            imgPath = os.path.join(labelImgPath,txt.split(".")[0]+".jpg")
            txtPath = os.path.join(labelImgPath,txt)
            if (not (os.path.exists(imgPath) and os.path.exists(txtPath))):
                try:
                    os.remove(imgPath)
                except:
                    print("no such file",imgPath)

                try:
                    os.remove(txtPath)
                except:
                    print("no such file",txtPath)


def removeTargetLabels(src):
    for file in os.listdir(src):
        if(file.endswith(".txt") and (not file.startswith("cl"))):
            filePath = os.path.join(src,file)
            labels = []
            with open(filePath,"r") as f:
                lines = f.readlines()
                for line in lines:
                    line = line.rstrip().split(" ")
                    labels.append(line[0])

                if not ("1" in labels):
                    try:
                        os.remove(filePath)
                        print(filePath,"has removed")

                    except:
                        print("has no such file:",filePath)
                    try:
                        os.remove(filePath.replace(".txt",".jpg"))
                        print(filePath.replace(".txt",".jpg"),"has removed")
                    except:
                        print("has no such file:",filePath)


def rot():
    path = "/mnt/data/people/peopleslice/use"
    for i in os.listdir(path):
        img = cv2.imread(os.path.join(path,i))
        img = np.rot90(img)
        cv2.imwrite("/mnt/data/people/peopleslice/userot/"+i,img)


def imgLabelPeople(src, dst="", labelPaths=""):
    labelImgsPath = []
    labelImgs = []

    ii = 0
    for root, dires, files in os.walk(labelPaths):
        for file in files:
            labelImgsPath.append(os.path.join(root, file))
            img = cv2.imread(os.path.join(root, file))

            labelImgs.append([img,root])
            print(file)
            ii += 1

    print("loaded all labelImg")
    total = len(labelImgs)

    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)
    random.shuffle(labelImgs)

    backImgFiles = os.listdir(src)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    random.shuffle(backImgFiles)
    num = 0

    for imgNum, backImgFile in enumerate(backImgFiles):
        if(not backImgFile.endswith(".jpg")):
            continue
        print (imgNum,backImgFile)
        backImg = cv2.imread(os.path.join(src, backImgFile))
        _,bw,bh = backImg.shape[::-1]
        try:
            partLabel = open("/mnt/data/people/peopleslice/labels/"+backImgFile.replace(".jpg",".txt"), "r")

            lines = partLabel.readlines()
            pLabels = []
            for line in lines:
                line = line.rstrip()
                line = line.split(" ")
                line = map(float, line)
                _, px, py, pw, ph = line
                rx,ry,rw,rh = vl.convertR(px,py,pw,ph,bw,bh)
                pLabels.append(map(int,[0,rx,ry,rw,rh]))
        except:
            pLabels = []

        _, bw, bh = backImg.shape[::-1]
        backImgResize = backImg  # cv2.resize(backImg, (vl.randInt(bw*0.7,bw*1.1),vl.randInt(bh*0.7,bh*1.1)))
        back = copy.copy(backImgResize)
        _, bw, bh = backImgResize.shape[::-1]
        with open(os.path.join(dst, backImgFile.split(".")[0] + ".txt"), 'w') as f:
            ratW, ratH = 1,1
            locs = []
            locs.extend(pLabels)

            for i in range(vl.randInt(1, 2)):
                if (num < total):
                    labelImg,labelSave = labelImgs[num]
                    if (i == 1):
                        labelImg,labelSave = labelImgs[vl.randInt(0, total / 2)]

                else:
                    num = 0
                    labelImg,labelSave = labelImgs[num]
                    if (i == 1):
                        labelImg,labelSave = labelImgs[vl.randInt(0, total / 2)]

                _, iw, ih = labelImg.shape[::-1]
                # labelImgResize = cv2.resize(labelImg, (int(iw * ratW), int(ih * ratH)))

                labelImgResize = labelImg
                # if(True):
                #     labelImgResize = cv2.resize(labelImg, (int(iw * ratW), int(ih * ratH)))
                _, lw, lh = labelImgResize.shape[::-1]

                _, lw, lh = labelImgResize.shape[::-1]

                try:
                    startX = vl.randInt(vl.randInt(15, 50), bw - lw)
                    startY = vl.randInt(vl.randInt(15, 50), bh - lh)
                except:
                   startX = 50
                   startY = 50

                locs.append([1,startX,startY,lw, lh])
                iouValue = nms(locs)
                # print("IOU=",iouValue,backImgFile)
                if(iouValue>0.1):
                    # print("IOU>0.2===========================",iouValue,backImgFile)
                    continue

                backImgResize[slice(startY, startY + lh), slice(startX, startX + lw)] = labelImgResize
                back[slice(startY, startY + lh), slice(startX, startX + lw)] = labelImgResize

                # x, y, w, h = startX, startY, lw, lh
                # rx, ry, rw, rh = vl.convert([bw, bh], [x, x + w, y, y + h])
                # labelMsg = " ".join(map(str, [str(labelSave), rx, ry, rw, rh])) + "\n"
                # f.write(labelMsg)
                # cv2.rectangle(back, (x, y), (x + w, y + h), (0, 0, 255), 2, 2)

            for loc in locs:
                label,x,y,w,h = loc
                rx, ry, rw, rh = vl.convert([bw, bh], [x, x + w, y, y + h])
                labelMsg = " ".join(map(str, [str(label), rx, ry, rw, rh])) + "\n"
                f.write(labelMsg)


        if (os.path.isfile(os.path.join(dst, backImgFile.split(".")[0] + ".txt"))):
            with open(os.path.join(dst, backImgFile.split(".")[0] + ".txt")) as f:
                lines = f.readlines()
                if (len(lines) == 0):
                    os.remove(os.path.join(dst, backImgFile.split(".")[0] + ".txt"))
                else:
                    imgShow = cv2.resize(back, (960, 540))
                    cv2.imshow("img", imgShow)
                    cv2.imwrite(dst + "/" + backImgFile.split(".")[0] + ".jpg", backImgResize)
                    cv2.waitKey(1)
        num += 1


def main():
    src = "/mnt/data/people/peopleslice/peopleData"
    dst = "/mnt/data/people/train"
    makeYoloTrainDataFormat(src, dst)
    return

    # rot()
    # return

    src = "/mnt/data/people/peopleslice/peopleData"
    dst = "/mnt/data/people/train"
    makeYoloTrainDataFormat(src, dst)
    return

    src = "/mnt/data/Pyproject/videosFPS"
    dst = "/mnt/data/people/peopleslice/peopleData"
    labelPaths = "/mnt/data/people/peopleslice/userot"
    imgLabelPeople(src, dst, labelPaths)
    return

    rot()
    return

    src = "/mnt/data/people/backup/sample2_train"
    dst = "/mnt/data/people/backup/sample2_train"

    # src = "/mnt/data/people/sample_train"
    removeTargetLabels(src)
    # return

    # src = "/mnt/data/people/sample5_train"
    removeNullImgLabel(src)
    checklabel(src, "/mnt/data/people/tmp")

    # vl.removeSmallImage(src,dst)
    return

    src = "/mnt/data/people/sample_train"
    removeTargetLabels(src)
    # return

    # src = "/mnt/data/people/sample5_train"
    removeNullImgLabel(src)
    # return

    checklabel(src, "/mnt/data/people/tmp")

    # src = "/mnt/data/people/sample_train"
    dst = "/mnt/data/people/train"
    makeYoloTrainDataFormat(src, dst)
    return

    # src = "/mnt/data/people/sample3_train"
    # dst = "/mnt/data/people/sliceImages"
    # checklabel(src, dst)
    # return

    src = "/mnt/data/people/sample3_train"
    removeNullImgLabel(src)
    return

    # src = "/mnt/data/people/sample4_train"
    src = "/mnt/data/people/tmp/sample_sample2_new_bak"
    dst = "/mnt/data/people/sliceImages"
    checklabel(src, dst)
    return


    src = "/mnt/data/people/sample4_train"
    dst = "/mnt/data/people/train"
    makeYoloTrainDataFormat(src, dst)
    return

    src = "/mnt/data/people/tmp2"
    removeNullImgLabel(src)
    return

    src = "/mnt/data/people/tmp2"
    removeTargetLabels(src)
    return

    src = "/mnt/data/people/tmp/sample_sample2_new_bak"
    dst = "/mnt/data/people/train"
    makeYoloTrainDataFormat(src, dst)
    return

    src = "/mnt/data/people/tmp/sample_sample2_new_bak"
    removeNullImgLabel(src)
    return

    src = "/mnt/data/people/tmp/sample_sample2_new"
    dst = "/mnt/data/people/train"
    makeYoloTrainDataFormat(src, dst)
    return


    src = "/mnt/data/people/tmp/sample_sample2_new"
    dst = "/mnt/data/people/sliceImages"
    checklabel(src, dst)
    return


    labelImgPath = "/mnt/data/people/tmp/sample_sample2_new"
    removeNullImgLabel(labelImgPath)
    return

    src = "/mnt/data/people/tmp/sample_sample2_new"
    removeTargetLabels(src)
    return

    src = "/mnt/data/people/tmp/sample_sample2"
    dst = "/mnt/data/people/tmp/sample_sample2_new"
    vl.removeSmallImage(src,dst)
    return

    labelImgPath = "/mnt/data/people/sample3/images"
    removeNullImgLabel(labelImgPath)
    return

    src = "/mnt/data/people/sample3/images"
    vl.removeSmallImage(src)
    return

    src = "/mnt/data/people/sample_sample2_train"
    dst = "/mnt/data/people/sliceImages"
    checklabel(src,dst)
    return

    src = "/mnt/data/people/tmp"
    dst = "/mnt/data/people/train"
    makeYoloTrainDataFormat(src, dst)
    return

    labelImgPath = "/mnt/data/people/tmp"
    removeNullImgLabel(labelImgPath)
    return

    src = "/mnt/data/people/20200219"
    dst = "/mnt/data/people/train"
    makeYoloTrainDataFormat(src, dst)
    return

    # img = cv2.imread("/mnt/data/people/倒地人物/001.png")
    # cv2.imshow("img",img)
    # cv2.waitKey()
    # return

    src = "/mnt/data/people/20200219"
    dst = "/mnt/data/people/tmp"
    checkLabelSlice(src,dst)
    return

    labelImgPath = "/mnt/data/people/20200222"
    removeNullImgLabel(labelImgPath)
    return

    labelImgPath = "/mnt/data/people/20200219"
    removeNullImgLabel(labelImgPath)
    return

    src = "/mnt/data/people/20200219"
    dst = "/mnt/data/people/train"
    makeYoloTrainDataFormat(src, dst)
    return

    src = "/mnt/data/people/20200219Agu"
    dst = "/mnt/data/people/train"
    makeYoloTrainDataFormat(src, dst)
    return

    labelImgPath = "/mnt/data/people/tmp/"
    removeNullImgLabel(labelImgPath)
    return

    src = "/mnt/data/people/20200219Agu"
    dst = "/mnt/data/people/train"
    makeYoloTrainDataFormat(src, dst)
    return

    labelImgPath = "/mnt/data/people/20200219Agu"
    removeNullImgLabel(labelImgPath)
    return


    src = "/mnt/data/people/20200222"
    dst = "/mnt/data/people/train"
    makeYoloTrainDataFormat(src, dst)
    return


    labelImgPath = "/mnt/data/people/downLoadsImg1029/tmp"
    removeNullImgLabel(labelImgPath)
    return


    src = "/mnt/data/people/20200219"
    dst = "/mnt/data/people/train"
    makeYoloTrainDataFormat(src,dst)
    return

    labelImgPath = "/mnt/data/people/downLoadsImg1029/tmp"
    removeNullImgLabel(labelImgPath)
    return

    labelImgPath = "/mnt/data/people/tmp/"
    removeNullImgLabel(labelImgPath)
    return

    srcTrain = "/home/tsl/face/images_selected/trainImagesLabelsAll"
    valTain = "/home/tsl/face/images_selected/valImagesLabelsAll"
    dst = "/home/tsl/face/images_selected/trainList"
    makeYoloTrainDataFormat(valTain,dst)
    return

    srcTrain = "/home/tsl/face/images_selected/trainImagesLabelsAll"
    dstTrain = "/home/tsl/face/images_selected/labelsTrainSlice"

    srcVal = "/home/tsl/face/images_selected/valImagesLabelsAll"
    dstVal = "/home/tsl/face/images_selected/labelsValSlice"

    checkLabelSlice(srcVal,dstVal)
    return

    # srcTrain = "/home/tsl/face/images_selected/trainImagesLabelsAll"
    srcVal = "/home/tsl/face/images_selected/valImagesLabelsAll"
    dst = "/home/tsl/face/images_selected/tmp"
    removeImgByLabel(srcVal,dst)
    return
    
    # src = "/mnt/data/Pyproject/advTrainImage0103"
    # src = "/mnt/data/Pyproject/clothes1121changto0103"
    # src = "/mnt/data/1104/videoFPS_Label/trainData0103"
    src = "/mnt/data/Pyproject/head/head_data/SCUT_HEAD_Part_B/JPEGImages"
    dst = "/mnt/data/Pyproject/trainVallist"
    makeYoloTrainDataFormat(src, dst)
    return

    src = "/mnt/data/Pyproject/videosFPS"
    dst = "/mnt/data/Pyproject/advTrainImage0103"
    labelPaths = "/mnt/data/Pyproject/searchAdv/checkLabelsAll/0103/advAgutation"
    imgLaLabelDire(src,dst,labelPaths)
    return


    # src = "/mnt/data/videos/qtx3"
    src = "/mnt/192.168.0.100.data/boat1126"
    dst = "/mnt/data/videos/labels"
    makeYoloTrainDataFormat(src, dst)
    return

    src = "/mnt/data/Pyproject/clothes1121"
    dst = "/mnt/data/Pyproject/clothesLable"
    makeYoloTrainDataFormat(src, dst)
    return

    src = "/mnt/data/Pyproject/videosFPS"
    dst = "/mnt/data/Pyproject/clothes1121"
    labelPaths = "/mnt/data/Pyproject/clothesTrainData/labelPaths/test"
    imgLaLabelDire(src,dst,labelPaths)
    return

    test("/mnt/data/Pyproject/search/train0923/baiduSearchDataLabel0923/000000002.jpg")
    return

    src = "/mnt/data/Pyproject/clothesTrainData/trainData/"
    dst = "/mnt/data/Pyproject/clothesLable"
    makeYoloTrainDataFormat(src,dst)
    return

    src = "/mnt/data/Pyproject/videosFPS"
    dst = "/mnt/data/Pyproject/clothesTrainData/trainData"
    labelPaths = "/mnt/data/Pyproject/clothesTrainData/labelPaths"
    imgLabelHoldOnLabelSize(src,dst,labelPaths)
    return

    # src = "/mnt/data/Pyproject/clothes"
    # dst = "/mnt/data/Pyproject/clothesTrainData"
    # removeImgByLabel(src, dst)
    # return

    src = "/mnt/data/Pyproject/clothes"
    dst = "/mnt/data/Pyproject/clothesTrainData"
    vl.getRoi(src,dst)
    return

    # src = '/mnt/data/Pyproject/searchGarbage/image360/360晒被子'
    # dst = "/mnt/data/Pyproject/clothes"
    # renameTrainData(src, dst)
    # return

    # src = "/mnt/data/Pyproject/clothes"
    # dst = "/mnt/data/Pyproject/clothesLable"
    # makeYoloTrainDataFormat(src,dst)
    # return


    src = "/mnt/data/Pyproject/searchGarbage/imagesBaidu/clothes/baidu晒衣服照片"
    dst = "/home/tsl/project_on/ships/tmp"
    removeImgByLabel(src, dst)
    return

    src = "/mnt/data/ships/fps/"
    src2 = "/home/tsl/ships/label_2/"

    dst = "/home/tsl/ships"
    fps2video(src, src2, dst)
    return

    src = "/home/tsl/ships/labels_20190731/"
    removeSameLabel(src)
    return

    src = "/mnt/data/Pyproject/searchGarbage/imagesBaidu/clothes/baidu-冬天晒衣服"
    dst = "/mnt/data/Pyproject/searchGarbage/imagesBaidu/clothes/tmp"
    vl.removeImg(src, dst)
    return

    # tmp()
    return

    # readFPSFromVideos()
    # return

    src = "/mnt/data/1104/videoFPS/1"
    dst = "/mnt/data/1104/videoFPS/1_labelsRoi"
    # vl.getRoi(src,dst)
    return

    src = "/mnt/data/Pyproject/rubbish1030/videoFPS1031-2/"
    dst = "/mnt/data/Pyproject/rubbish1030/videoFPS1031-2LabelsImg"
    # checklabel(src,dst)

    src = "/mnt/data/Pyproject/searchGarbage/image360/beizi1"
    # dst = "/mnt/data/Pyproject/searchGarbage/image360/quitLabelsRoi1003"
    # checklabel(src,dst)

    # vl.removeSmallImage(dst+'/quilt')

    src = "/home/tsl/zzm/videosFPS/"
    dst = "/home/tsl/zzm/videosFPSLabel"
    # checklabel(src,dst)

    # src = "train0923/360SearchDataLabel0923"
    # dst = 'train0923/360SearchDataLabel0923Roi'
    src = "./downLoadsImg1029/used"
    dst = "./downLoadsImg1029/labelUsed"
    # checklabel(src,dst)

    src = "/mnt/data/Pyproject/rubbishTrainImages1029/1025kerasAgu"
    dst = "/mnt/data/Pyproject/rubbishTrainImages1029/1025kerasAguRubbishTrainImages"
    # checklabel(src,dst)

    # src = "train0923/baiduSearchDataLabel0923"
    # dst = 'train0923/baiduSearchDataLabel0923Roi'
    # checklabel(src, dst)

    # src = "train0923/shenxia"
    # dst = 'train0923/shenxiaRoi'
    # checklabel(src, dst)

    src = "/mnt/data/Pyproject/searchAdv/downLoadsImg1018/0"
    dst = "/mnt/data/Pyproject/searchAdv/checkLabelsAll/1029/"
    # checklabel(src, dst)

    src = "/mnt/data/Pyproject/searchAdv/downLoadsImg1017/save"
    dst = "checkOut1018/"
    # checklabel(src, dst)

    # removeSmallImage("temp")
    # renameLabel(src)

    # dst = ""
    # src = "/mnt/data/Pyproject/searchGarbage/labels1028"
    # src = "/mnt/47.99.32.39/data/advTrainImage1029"
    src = "/mnt/data/Pyproject/advTrainImage1029"
    # makeYoloTrainDataFormat(src,dst)
    # tmp()

    # videoLabel()
    src = "/mnt/data/Pyproject/videoJPG"
    dst = "/mnt/data/Pyproject/bagTrainImages1029"
    # imgLabel(src,dst)

    # src = "/mnt/data/Pyproject/videosFPS"
    # dst = "/mnt/data/Pyproject/rubbishTrainImages1029/1025kerasAguRubbishTrainImages/rubbishTrainData"
    # labelPaths = "/mnt/data/Pyproject/rubbishTrainImages1029/1025kerasAguRubbishTrainImages/rubbishAgutation"
    # imgLabel(src,dst,labelPaths)
    src = "/mnt/data/Pyproject/videosFPS"
    dst = "/mnt/data/Pyproject/searchGarbage/bag/bagTrainImages"
    labelPaths = "/mnt/data/Pyproject/searchGarbage/bag/bagTrainImageslabel"
    # imgLabel(src, dst, labelPaths)

    src = "/mnt/data/Pyproject/videosFPS"
    dst = "/mnt/data/Pyproject/advTrainImage1029"
    labelPaths = "/mnt/data/Pyproject/searchAdv/checkLabelsAll/1029/totalAdv"
    # imgLabel(src, dst, labelPaths)

    src = "/mnt/data/Pyproject/videosFPS"
    dst = "/mnt/data/Pyproject/rubbish1030/videosFPSLabel/traingBag1030"
    labelPaths = "/mnt/data/Pyproject/rubbish1030/videosFPSLabel/kerasAgutation"
    # imgLabel(src, dst, labelPaths)

    src = "/mnt/data/Pyproject/videosFPS"
    dst = "/mnt/data/Pyproject/quilt1103"
    labelRoiPaths = "/mnt/data/Pyproject/searchGarbage/image360/quitLabelsRoi1103/quiltKerasAgutation"
    # imgLabel(src, dst, labelRoiPaths)
    # vl.checkTrainValLabels(labelRoiPaths)
    # vl.removeNullTxt(labelPaths)
    dst = "/mnt/data/Pyproject/searchGarbage/image360/beizi1/"
    # vl.checkOutLabels(dst)
    # vl.checkTrainValLabels(labelPaths)
    src = "/mnt/data/Pyproject/quilt1103"
    # makeYoloTrainDataFormat(src)

    # src = "labels1017"
    # src = "labels1018"
    # src = "labels1021"
    src = "/mnt/data/Pyproject/searchGarbage/tmp"
    src = "/mnt/data/Pyproject/bagTrainImages1029"
    # makeYoloTrainDataFormat(src)
    src = "/mnt/data/Pyproject/rubbishTrainImages1029/1025kerasAguRubbishTrainImages/rubbishTrainData"
    # src = "/mnt/data/Pyproject/searchGarbage/bag/bagTrainImages"

    src = "/mnt/data/Pyproject/rubbishTrainImages1029/1025kerasAguRubbishTrainImages/rubbishTrainData"
    # makeYoloTrainDataFormat(src)
    src = "/mnt/data/Pyproject/rubbish1030/videosFPSLabel/traingBag1030"
    # makeYoloTrainDataFormat(src)

    src = "/mnt/data/Pyproject/searchGarbage/bag/bagTrainImages"
    src = "/home/tsl/zzm/videosFPS/"
    # vl.checkOutLabels(src)

    srcTrainLabel = "/mnt/data/Pyproject/searchAdv/trainVal/train1022.txt"
    srcValLabel = "/mnt/data/Pyproject/searchAdv/trainVal/val1022.txt"
    # vl.checkTrainValLabels(srcValLabel)

    src = "tmp"
    dst = ""
    # vl.gauAugmentation(src, dst)

    src = "temp/temp1018"
    dst = "classTrainData/adv"
    # vl.mkClassData(src,dst,data=10182)
    # checklabel("/mnt/data/Pyproject/searchGarbage/downLoadsImg1024/0","temp")

    # renameEnds("/mnt/data/Pyproject/searchGarbage/downLoadsImg/1025")

    src = "/mnt/data/Pyproject/searchGarbage/labels1024/"
    # readData(src)

    # src = "/mnt/data/videos/0903"
    src = "/mnt/data/videos/视频1015"
    dst = "/mnt/data/Pyproject/videosFPS"
    # readFrame(src,dst)

    src = "/mnt/data/Pyproject/rubbish1030/videoFPS1031-2"
    # vl.checkOutLabels(src)
    # vl.checkSame(src)


if __name__ == '__main__':
    main()
